#!/bin/bash
#SBATCH --spread-job
# Copyright (c) 2020 Presto Labs Pte. Ltd.

export OUTDIR="{{out_dir}}"
mkdir -p "${OUTDIR}"

if [ -z ${SLURM_JOB_ID} ]; then
  (>&2 echo "ERROR: run this script on slurm.")
  exit 1
fi

set -e
source /remote/iosg/home-8/bot-hft/miniconda3/etc/profile.d/conda.sh
conda activate /remote/iosg/home-8/bot-hft/miniconda3/envs/coin2_motion_env_migration

cd /remote/iosg/home-8/bot-hft/jenkins/workspace/coin2_feed_stats/coin/python

# set env
source ${CONDA_PREFIX}/opt/hyper/bin/envsetup.sh
source ${CONDA_PREFIX}/opt/papyrus/bin/envsetup.sh
REPO_ROOT="$(git rev-parse --show-toplevel)"
export BATCH_INSTALL_ROOT=${BATCH_INSTALL_ROOT:-"${REPO_ROOT}/external"}

NUM_PARALLEL="$((${SLURM_NTASKS} + 30))"
parallel -j "${NUM_PARALLEL}" --delay {{job_submit_period}} <<EOF
{% for trading_date in trading_dates -%}
{% for machine in settings.keys() -%}
{% for mea in settings[machine].keys() -%}
{% for recipe in settings[machine][mea].keys() -%}
{% for group in settings[machine][mea][recipe].keys() -%}
{% for worker in settings[machine][mea][recipe][group]["workers"] -%}
{% if mea in ['Futures.Binance.v1','Futures.Binance.v1-delivery','Futures.Ftx.v1','Futures.Okex.v5','Futures.Okex.v5-swap','Spot.Binance.v1','Spot.Ftx.v1','Spot.Gdax.v1'] and recipe in ['realtime','non_tbs']-%}
srun -N1 -n1 -l --mem-per-cpu=2G --exclusive ./pyrunner coin/support/feed_tool/feed_stats/app/feed_stats_motion/feed_stats_single_process_runner.py --trading_date="{{trading_date}}" --machine="{{machine}}" --mea="{{mea}}" --recipe="{{recipe}}" --group="{{group}}" --worker={{worker}} --interval="{{interval}}" --run_for_raw={{settings[machine][mea][recipe][group]["raw"]}}
{% else -%}
srun -N1 -n1 -l --mem-per-cpu=2G --exclusive ./pyrunner coin/support/feed_tool/feed_stats/app/feed_stats_motion/feed_stats_single_process_runner.py --trading_date="{{trading_date}}" --machine="{{machine}}" --mea="{{mea}}" --recipe="{{recipe}}" --group="{{group}}" --worker={{worker}} --run_for_raw={{settings[machine][mea][recipe][group]["raw"]}}
{% endif -%}
{% endfor -%}
{% endfor -%}
{% endfor -%}
{% endfor -%}
{% endfor -%}
{% endfor -%}
EOF

cd /remote/iosg/home-8/bot-hft/jenkins/workspace/coin2_feed_stats/coin/python
python -m coin.tool.mailer --name="coin-feed-stats" --mailfrom="coin-sim@joomo.io" --mailto="{{summary_mailto}}" --subject="{{mail_subject}}" <<EOF
Slurm job ID:     ${SLURM_JOB_ID}
Output directory: ${OUTDIR}
User tag:         None

---
$(scontrol show job ${SLURM_JOB_ID})
EOF
